package com.browsesoft.htmlcomponent;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.Serializable;
import java.io.StringReader;

import javax.servlet.ServletContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXParseException;

import com.browsesoft.listener.ContextListener;
import com.browsesoft.util.StreamHelper;

/**
 * 模板类，包括名字和内容
 */
public class Template implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 6129139229728823275L;

	/**
	 * 模板名字
	 */
	private String name = "";

	/**
	 * 模板的内容
	 */
	private String content = "";

	public void setName(String n) {
		this.name = n;
	}

	public String getName() {
		return name;
	}

	/**
	 * 生成模板的文档
	 */
	public Document getDocument() throws Exception {
		try {
			StringBuffer buffer = new StringBuffer(content);
			StringBuffer tempBuffer = null;
			// 替换<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0
			// Transitional//EN\"
			// \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">为""
			int index = buffer.indexOf("!DOCTYPE");
			if (index != -1) {
				tempBuffer = new StringBuffer(buffer.substring(index, buffer
						.length()));
				buffer = buffer.delete(index - 1, tempBuffer.indexOf(">")
						+ index + 1);
			}
			// 替换<meta http-equiv=\"Content-Type\" content=\"text/html;
			// charset=gb2312\"/>
			// 为""
			index = buffer.indexOf("meta");
			if (index != -1) {
				tempBuffer = new StringBuffer(buffer.substring(index, buffer
						.length()));
				buffer = buffer.delete(index - 1, tempBuffer.indexOf(">")
						+ index + 1);
			}
			// 替换<html xmlns=\"http://www.w3.org/1999/xhtml\">为<html>
			index = buffer.indexOf("html xmlns");
			if (index != -1) {
				tempBuffer = new StringBuffer(buffer.substring(index, buffer
						.length()));
				buffer = buffer.delete(index + 4, tempBuffer.indexOf(">")
						+ index);
			}
			// 替换&nbsp;为空格引用
			while (buffer.indexOf("&nbsp;") != -1) {
				index = buffer.indexOf("&nbsp;");
				buffer.delete(index, index + 6);
			}
			String context = buffer.substring(0);
			DocumentBuilder builder = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder();
			return (Document) builder.parse(new InputSource(new StringReader(
					context)));
		} catch (SAXParseException e) {
			System.out.println("模板" + this.getName() + "不符合XML规范，错误位置为："
					+ e.getLineNumber() + "行" + e.getColumnNumber() + "列" + "，"
					+ "错误内容为：" + e.getMessage());
			throw e;
		}
	}

	/**
	 * 得到资源流
	 * 
	 * @param path
	 * @throws Exception
	 */
	private InputStream getInput(String path) throws FileNotFoundException {
		ServletContext context = ContextListener.getContext();
		InputStream is = context.getResourceAsStream(path
				+ name.concat(".html"));
		if (is != null) {
			return is;
		}
		// 继续看有没有htm
		is = context.getResourceAsStream(path + name.concat(".htm"));
		if (is != null) {
			return is;
		}
		throw new FileNotFoundException(name);
	}

	public void setContent(String path) throws Exception {
		// 构造输入流
		InputStream is = this.getInput(path);
		content = StreamHelper.getString(is);
		is.close();
	}
}